package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.CustomerService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping(value = "/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @GetMapping
    public Return<List<Customer>> query(Customer customer,PageInfo info){
        Return<List<Customer>> ret=new Return<>();
        List<Customer> customers=customerService.query(customer);
        ret.getCommon().setRet(true);
        ret.setData(customers);
        return ret;
    }
    @GetMapping(value = "/vip")
    public Return<List<Customer>> queryVip(PageInfo info){
        Return<List<Customer>> ret=new Return<>();
        List<Customer> customers=customerService.queryVip();
        ret.getCommon().setRet(true);
        ret.setData(customers);
        return ret;
    }
    @GetMapping(value = "/registerCustomer")
    public Return<List<Customer>> queryRegisterCustomer(PageInfo info){
        Return<List<Customer>> ret=new Return<>();
        List<Customer> customers=customerService.queryRegisterCustomer();
        if(customers!=null&&customers.size()>0)
        ret.getCommon().setRet(true);
        ret.setData(customers);
        return ret;
    }

    @ApiOperation(value = "新增用户", notes = "新增用户")
    @ApiImplicitParam(name = "customer", value = "用户对象", required = true, dataType = "Object")
    @PostMapping
    public Return<Customer> save(Customer customer){
        Return<Customer> ret=new Return<>();
        if(customerService.queryByOpenid(customer.getOpenid())==null){
            int i=customerService.insert(customer,ret);
            if (i>0) {
                ret.getCommon().setRet(true);
            }
        }
        return ret;
    }

    @ApiOperation(value = "查询用户", notes = "根据openid查询用户")
    @ApiImplicitParam(name = "openid", value = "用户对象", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{openid}")
    public Return<Customer> queryByOpenid(@PathVariable(value = "openid") String openid){
        Return<Customer> ret=new Return<>();
        Customer customer=customerService.queryByOpenid(openid);
        if(customer!=null){
            ret.getCommon().setRet(true);
            ret.setData(customer);
        }

        return ret;
    }

    @GetMapping(value = "/byId/{customerId}")
    public Return queryById(@PathVariable(value = "customerId") String customerId){
        Return<Customer> ret=new Return<>();
        Customer customer=customerService.queryById(customerId);
        if(customer!=null){
            ret.getCommon().setRet(true);
        }
        ret.setData(customer);
        return ret;
    }

    @PostMapping(value = "/upLevel")
    public Return upLevel(UpLevelParams upLevelParams){
        Return ret=new Return();
        if(StringUtils.isEmpty(upLevelParams.getPhone())){
            ret.getCommon().setMessage("请输入手机号");
            return ret;
        }
        if(customerService.upLevel(upLevelParams)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @GetMapping(value = "/export")
    public void export(Customer customer, HttpServletResponse response) throws IOException {
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("会员信息.xlsx", "utf-8"));
        customerService.export(customer,response);
    }


}
